package com.gxljc.bear.crawler.itaogao.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;

import com.gxljc.bear.crawler.base.DolphinCrawlerConsts;
import com.gxljc.bear.crawler.base.DolphinFetchData;
import com.gxljc.bear.crawler.itaogao.ItaogaoConst;
import com.gxljc.bear.crawler.itaogao.NewsPageTable;

/**
 * @author haitao E-mail:haitao@summba.com
 * @version createTime：2021年9月19日 上午9:33:26
 * 
 */

public class HttpUtil {
	private final static Logger LOG = Logger.getLogger(HttpUtil.class);
	/**
	 * 判别url的Status是否正常
	 * 
	 * @param URLName
	 * @return
	 */
	public static boolean checkUrlexists(String URL) {
		try {
			HttpURLConnection.setFollowRedirects(false);
			HttpURLConnection con = (HttpURLConnection) new URL(URL).openConnection();
			// GET POST HEAD OPTIONS PUT DELETE TRACE
			con.setRequestMethod("HEAD");
			con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); 
			return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 抓取网页 httpClient
	 *
	 * @param urlPath
	 * @param charset
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	@SuppressWarnings({})
	public static String getHtmlContent(String urlPath, String charset) throws ClientProtocolException, IOException {
		HttpClient httpClient = HttpClients.custom().setMaxConnTotal(400).setMaxConnPerRoute(400)
				.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(5000).build())
				.setRetryHandler(new DefaultHttpRequestRetryHandler(5, true)).build();
		HttpGet request = new HttpGet(urlPath);
		HttpResponse response = httpClient.execute(request);
		// 服务请求状态码小于四百的为正常
		if (response.getStatusLine().getStatusCode() < 400) {
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
			String line = "";
			StringBuffer sb = new StringBuffer();
			while ((line = rd.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		} else {
			return null;
		}
	}

	//把图片列表放到内存后面
	public static void insertImage2Content(NewsPageTable table) {
		List<String> images = table.images;
		if (CollectionUtils.isEmpty(images)) return;
		String content = table.content;
		if (StringUtils.isEmpty(content)) return;
		StringBuffer sb = new StringBuffer();
		for (String image : images) {
			String newImage = String.format(ItaogaoConst.IMAGE_HTML_FORMAT, image);
			sb.append(newImage);
		}
		content = content + DolphinCrawlerConsts.DEFAULT_LINE_BREAK_WORD + sb.toString();
		table.setContent(content);
	}
	
	public static byte[] returnHtmlByte(String seed,boolean proxy) throws Exception {
		DolphinFetchData fetchData = DolphinFetchData
				.getInstance(DolphinCrawlerConsts.CrawlerChannelType.bear.getName());
		byte[] htmlByte = fetchData.getHtml(seed, proxy, ItaogaoConst.CRAWL_TIMEOUT);
		if (htmlByte == null) {
			LOG.error("page result is empty url = " + seed);
			return null;
		}
		return htmlByte;
	}
}
